package com.example.xungeng2.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xungeng2.common.Result;
import com.example.xungeng2.pojo.Patrol;
import com.example.xungeng2.pojo.Shiduan;
import com.example.xungeng2.pojo.Work;
import com.example.xungeng2.service.PatrolService;
import com.example.xungeng2.service.ShiduanService;
import com.example.xungeng2.service.WorkService;
import com.example.xungeng2.unknow.TableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
public class PatrolController {
    @Autowired
    private PatrolService patrolService;

    @Autowired
    private WorkService workService;

    @Autowired
    private ShiduanService shiduanService;

    @Cacheable(value = "patrol",key = "#limit")
    @GetMapping("/api/patrol/selectPage")
    public TableMap selectPage(long page, long limit) {
        Page<Patrol> patrolPage = patrolService.selectPage(page, limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(patrolPage.getRecords());
        tableMap.setCount(patrolPage.getTotal());
        return tableMap;
    }

    @CacheEvict(value = "patrol",allEntries = true)
    @PostMapping("/Work/insert")
    public Result insert(Patrol patrol) throws ParseException {
        System.out.println(patrol.toString());
        Patrol patrol1 = new Patrol();
        patrol1.setPatrolSum(12);
        patrol1.setTotal(19);
        patrol1.setTaskSum(13);
        patrol1.setTaskSum(9);
        patrol1.setNotSum(4);
        patrol1.setTodaySum(12);
        boolean save = patrolService.save(patrol1);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @CacheEvict(value = "work",allEntries = true)
    @PostMapping("/worker/insert")
    public Result insertinfo(Work work){
        System.out.println(work);
        Work work1 = new Work();
        work1.setWorking(1);
        work1.setTaskNum(10);
        work1.setNotNum(5);
        work1.setWorkId(0);
        boolean save = workService.save(work1);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/api/patrol/selectByTime")
    public TableMap selectByTime(long page,long limit){
        Page<Shiduan> shiudan = shiduanService.selectAll(page,limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(shiudan.getRecords());
        tableMap.setCount(shiudan.getTotal());
        return tableMap;
    }
}